package com.bw.shop2208a.home

import android.content.Context
import android.widget.ImageView
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.bw.shop2208a.R
import com.bw.shop2208a.adapter.GoodsAdapter
import com.bw.shop2208a.adapter.GoodsTypeAdapter
import com.bw.shop2208a.databinding.FragmentHomeBinding
import com.bw.shop2208a.mvvm.BaseFragment
import com.bw.shop2208a.utils.GlideUtils
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.youth.banner.BannerConfig
import com.youth.banner.loader.ImageLoader

class HomeFragment : BaseFragment<FragmentHomeBinding, HomeViewModel>() {
    var BannerList = mutableListOf<String>()
    var goodsAdapter = GoodsAdapter()
    var goodsTypeAdapter = GoodsTypeAdapter()
    var page = 1
    var size = 10
    var cate = 14
    override fun initViwe() {
        viewModel.getBanner()
        viewModel.getGoodsType()
        binding.rvCate.layoutManager = GridLayoutManager(context, 5)
        binding.rvCate.adapter = goodsTypeAdapter
        viewModel.getGoods(cate, page, size)
        binding.rvGoods.layoutManager = GridLayoutManager(context, 2)
        binding.rvGoods.adapter = goodsAdapter
        goodsTypeAdapter.setOnItemClickListener { adapter, view, position ->
            page = 1
            val entity = goodsTypeAdapter.data[position]
            cate = entity.id
            goodsAdapter.data.clear()
            viewModel.getGoods(cate, page, size)
        }
        binding.srl.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                page = 1
                viewModel.getGoods(cate, page, size)
                binding.srl.finishRefresh()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                viewModel.getGoods(cate, page++, size)
                binding.srl.finishLoadMore()
            }
        })
        goodsAdapter.setOnItemClickListener { adapter, view, position ->
            val get = goodsAdapter.data[position]
            ARouter.getInstance().build("/app/goods").withSerializable("goods",get).navigation()
        }

        binding.query.setOnClickListener {
            ARouter.getInstance().build("/app/search").navigation()
        }
    }

    override fun iniData() {
        viewModel.successBanner.observe(this) {
            if (it.code == 200) {
                it.data.forEach {
                    BannerList.add(it.imagePath)
                }
                binding.ban.setImages(BannerList)
                    .setImageLoader(object : ImageLoader() {
                        override fun displayImage(
                            context: Context?,
                            path: Any?,
                            imageView: ImageView?
                        ) {
                            if (context != null && imageView != null) {
                                GlideUtils.loadImage(context, imageView, path.toString())
                            }
                        }
                    }).setDelayTime(2000).setBannerStyle(BannerConfig.NUM_INDICATOR)
                    .start()
            }
        }

        viewModel.successGoodsType.observe(this) {
            if (it.code == 200) {
                val goodsTypeEntity = it.data[0].data
                goodsTypeAdapter.data.addAll(goodsTypeEntity)
                goodsTypeAdapter.notifyDataSetChanged()
            }
        }

        viewModel.successGoods.observe(this) {
            if (it.code == 200) {
                if (page==1){
                    goodsAdapter.data.clear()
                }
                goodsAdapter.data.addAll(it.data)
                goodsAdapter.notifyDataSetChanged()


            }
        }
    }

    override fun getLayoutId(): Int = R.layout.fragment_home

    override fun getViewModel(): Class<HomeViewModel> = HomeViewModel::class.java


}